package cn.edu.nchu.fragmentdemo.adapter;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;
import cn.edu.nchu.fragmentdemo.R;
import cn.edu.nchu.fragmentdemo.bean.WayPoint;
import cn.edu.nchu.fragmentdemo.db.helper.ORMLiteHelper;

/**
 * @className: WayPointAdapter
 * @description: TODO 类描述
 * @author: Chenzilei
 * @date: 2023/10/12 11:32
 **/
public class WayPointAdapter extends RecyclerView.Adapter<WayPointAdapter.WayPointViewHolder> {
    private Context context;
    private List<WayPoint> wayPoints;
    private int selectedPosition = -1; // 默认没有选中任何按钮
    private SQLiteDatabase db;
    private OnButtonClickListener onButtonClickListener;
    private Integer showFirstWayPoint; //确认当前航线是否有航点


    public WayPointAdapter(Context context, List<WayPoint> wayPoints, OnButtonClickListener onButtonClickListener, Integer showFirstWayPoint) {
        this.context = context;
        this.wayPoints = wayPoints;
        this.onButtonClickListener = onButtonClickListener;
        this.showFirstWayPoint=showFirstWayPoint;
        // 初始化数据库
        ORMLiteHelper dbHelper = new ORMLiteHelper(context);
        db = dbHelper.getReadableDatabase();
    }

    @NonNull
    @Override
    public WayPointViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.waypoint_button, parent, false);
        return new WayPointViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final WayPointViewHolder holder, final int position) {

        WayPoint waypoint = wayPoints.get(position);
        int row = position / 7; // 计算行号
        int col = position % 7; // 计算列号
        // 设置按钮的文本和点击事件等
        holder.textView.setText("" + (position + 1));
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理按钮点击事件
                handleButtonClick(position);
                // 调用接口的方法，将按钮的position传递给Fragment
                if (onButtonClickListener != null) {
                    onButtonClickListener.onButtonClick(position);
                }
            }
        });

        // 根据选中状态设置按钮的颜色
        if (selectedPosition == position) {
            holder.button.setImageResource(R.drawable.waypoint_button_selected);
        } else {
            holder.button.setImageResource(R.drawable.waypoint_button_unselected);
        }


        // 设置按钮的布局参数，使其在对应的行和列中显示
        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) holder.button.getLayoutParams();
        layoutParams.setMargins(0, row*20, 0, 0);
        holder.button.setLayoutParams(layoutParams);

        if(showFirstWayPoint==1)
        {
            if (position == 0) {
                holder.button.post(new Runnable() {
                    @Override
                    public void run() {
                        holder.button.performClick();
                    }
                });
            }
            showFirstWayPoint=2;
        }

    }

    @Override
    public int getItemCount() {
        return wayPoints != null ? wayPoints.size() : 0;
    }

    class WayPointViewHolder extends RecyclerView.ViewHolder {
        ImageButton button;
        TextView textView;
        RelativeLayout relativeLayout;

        WayPointViewHolder(View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.imageButton);
            textView = itemView.findViewById(R.id.textView);
            relativeLayout = itemView.findViewById(R.id.button_textView);
        }
    }

    public void handleButtonClick(int position) {
        if (selectedPosition == position) {
            // 如果点击的是已选中的按钮，则取消选中状态
//            selectedPosition = -1;
        } else {
            // 更新选中的按钮位置
            selectedPosition = position;
        }
        // 刷新RecyclerView以更新按钮的颜色
        notifyDataSetChanged();
    }

    public interface OnButtonClickListener {
        void onButtonClick(int position);
    }

    public void setShowFirstWayPoint(Integer showFirstWayPoint) {
        this.showFirstWayPoint = showFirstWayPoint;
    }
}
//
//
//        // 执行数据库查询
//        String[] projection = {
//                "flight_speed",
//                "yaw_angle",
//                "hover_time",
//                "ascent_speed",
//                "descent_speed",
//                "waypoint_longitude",
//                "waypoint_latitude"
//        };
//        String selection = "id = ?";
//        String[] selectionArgs = { String.valueOf(waypoint.getId()) };
//        Cursor cursor = db.query(
//                "waypoint",
//                projection,
//                selection,
//                selectionArgs,
//                null,
//                null,
//                null
//        );
//
//        // 将查询结果赋值给对应的组件
//        if (cursor.moveToFirst()) {
//            int flightSpeed = cursor.getInt(cursor.getColumnIndexOrThrow("flight_speed"));
//            int yawAngle = cursor.getInt(cursor.getColumnIndexOrThrow("yaw_angle"));
//            int hoverTime = cursor.getInt(cursor.getColumnIndexOrThrow("hover_time"));
//            float ascentSpeed = cursor.getFloat(cursor.getColumnIndexOrThrow("ascent_speed"));
//            float descentSpeed = cursor.getFloat(cursor.getColumnIndexOrThrow("descent_speed"));
//            double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow("waypoint_longitude"));
//            double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow("waypoint_latitude"));
//
//            // 将值赋给对应的TextView和滑动条
//            // 找到UavCustomSeekbarWithCheckbox视图
//        UavCustomSeekbarWithCheckbox flightSpeedSeekBar = holder.itemView.findViewById(R.id.flightSpeed);
//
//        System.out.println("flightSpeedSeekBar = " + flightSpeedSeekBar);
////            // 设置新的进度值
////            int newProgress = 10; // 替换为您想要设置的进度值
////            flightSpeedSeekBar.setProgressValue(newProgress);
//
//            // ...
//
//        }
//
//        cursor.close();